package cn.com.xiaoshoujia.quartz.wx;

import cn.com.xiaoshoujia.activities.dto.ActivityRefundSmsDTO;
import cn.com.xiaoshoujia.application.service.SMSService;
import cn.com.xiaoshoujia.domain.enums.SignName;
import cn.com.xiaoshoujia.order.entity.Order;
import cn.com.xiaoshoujia.order.mapper.OrderMapper;
import cn.com.xiaoshoujia.order.mapper.RefundMapper;
import cn.com.xiaoshoujia.user.entity.ActivityTeamHead;
import cn.com.xiaoshoujia.user.mapper.ActivityTeamHeadMapper;
import cn.com.xiaoshoujia.utils.SerializationUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TeamHeadQuartz implements Job {

    private Logger logger = LoggerFactory.getLogger(TeamHeadQuartz.class);

    @Autowired
    private ActivityTeamHeadMapper activityTeamHeadMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RefundMapper refundMapper;

    @Autowired
    private SMSService smsService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Long teamId = dataMap.getLong("teamId");
        logger.info("team wx quartz teamId = {}", teamId);
        ActivityTeamHead activityTeamHead = activityTeamHeadMapper.findWxHeadById(teamId);
        if (activityTeamHead == null) {
            logger.info("team wx quartz activityTeamHead null");
        } else {
            logger.info("team wx quartz activityTeamHead {}", activityTeamHead.getStatus());
            logger.info("team wx quartz activityTeamHead boolean = {}", !activityTeamHead.getStatus().equals(3));
            if (!activityTeamHead.getStatus().equals(3) && !activityTeamHead.getStatus().equals(-1)) {
                activityTeamHead.setStatus(-1);
                int i = activityTeamHeadMapper.updateStatus(activityTeamHead);
                logger.info("team wx quartz activityTeamHead i = {}", i);
                if (i > 0) {
                    logger.info("team wx quartz teamId 该团因超时未成功已失败");
                    List<String> list = orderMapper.findByTeamId(teamId);
                    if (list != null && list.size() > 0) {
                        refundMapper.save(list);
                    }
                    logger.info("start update team no pay order");
                    int count = orderMapper.updateTeamStatusByHeadId(activityTeamHead.getId());
                    logger.info("team no pay order count = {}",count);
                    List<ActivityRefundSmsDTO> refundSmsDTOList = activityTeamHeadMapper.findHeadFail(activityTeamHead.getId());
                    logger.info("list {}",refundSmsDTOList);
                    if (null!=refundSmsDTOList&&refundSmsDTOList.size()>0){
                        logger.info("send msg {}",refundSmsDTOList);
                        Map<String, String> map = new HashMap<>();
                        map.put("name", StringUtils.substring(refundSmsDTOList.get(0).getTitle(), 0, 10));
                        String phone = refundSmsDTOList.get(0).getPhone();
                        if (StringUtils.isNotBlank(phone)){
                            String[] strs = SerializationUtils.getJsonArrStrFormStrings(phone);
                            if (null != strs)
                                map.put("contact",StringUtils.join(strs, ",") );
                            else
                                map.put("contact","--" );
                        }else{
                            map.put("contact","--" );
                        }
                        map.put("comname", StringUtils.substring(refundSmsDTOList.get(0).getSponsor(), 0, 10));
                        for (ActivityRefundSmsDTO activityRefundSmsDTO : refundSmsDTOList) {
                            logger.info("send message {}",activityRefundSmsDTO.getCluesPhone());
                            String s = smsService.sendMessage(activityRefundSmsDTO.getCluesPhone(), SignName.SHAREBANG, "SMS_147437983", map);
                            logger.info("sendMessage back mesaage {}",s);
                        }
                    }
                }
            }
        }
    }
}
